package com.gp.pj.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gp.pj.entity.MenuRole;
import com.gp.pj.entity.Role;
import com.gp.pj.service.MenuRoleService;
import com.gp.pj.service.RoleService;
import com.gp.pj.utils.ResultUtils;
import com.gp.pj.vo.MenuRoleVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chloe
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/")
public class RoleController {
    @Resource
    RoleService roleService;

    @Resource
    MenuRoleService menuRoleService;

    @ResponseBody
    @GetMapping(value = "/get_role_list")
    public ResultUtils roleList(){
        JSONArray data = roleService.roleList();
        return ResultUtils.success(data);
    }

    @ResponseBody
    @PostMapping(value = "/saveRole")
    public ResultUtils saveRole(@RequestBody JSONObject jsonRe){
        Role role = jsonRe.toJavaObject(Role.class);
        boolean result =  roleService.save(role);
        if (result) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("保存菜单失败");
        }
    }

    @ResponseBody
    @PostMapping(value = "/updateRole")
    public ResultUtils updateRole(@RequestBody JSONObject jsonRe){
        Role role = jsonRe.toJavaObject(Role.class);
        boolean result =  roleService.updateById(role);
        if (result) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("更新菜单失败");
        }
    }

    @ResponseBody
    @PostMapping(value = "/deleteRole")
    public ResultUtils deleteRole(@RequestBody JSONObject jsonRe){
        Role role = jsonRe.toJavaObject(Role.class);
        roleService.removeById(role.getId());
        Map<String, Object> fitter = new HashMap<>();
        fitter.put("role_Id",role.getId());
        boolean result = menuRoleService.removeByMap(fitter);
        if (result) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("保存菜单失败");
        }
    }

    @ResponseBody
    @PostMapping(value = "/updateRoleMenu")
    public ResultUtils updateRoleMenu(@RequestBody JSONObject jsonRe){
        AtomicReference<ResultUtils> data = new AtomicReference<>(new ResultUtils());
        MenuRoleVo menuRoleVo = jsonRe.toJavaObject(MenuRoleVo.class);
        JSONArray menuIds = menuRoleVo.getMenuIds();
        Map<String, Object> fitter = new HashMap<>();
        fitter.put("role_Id",menuRoleVo.getRoleId());
        menuRoleService.removeByMap(fitter);
        MenuRole saveRoleMenu = new MenuRole();
        if(!menuIds.isEmpty()){
            menuIds.forEach(item ->{
                saveRoleMenu.setMenuId(Integer.parseInt((String) item));
                saveRoleMenu.setRoleId(menuRoleVo.getRoleId());
                boolean result = menuRoleService.save(saveRoleMenu);
                if(result){
                    data.set(ResultUtils.success());
                }else{
                    data.set(ResultUtils.failure("绑定用户菜单失败"));
                }
            });
        }else{
            data.set(ResultUtils.success());
        }
        return data.get();
    }
}
